package com.ego.user.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ego.auth.entity.UserInfo;
import com.ego.user.interceptor.LoginInterceptor;
import com.ego.user.mapper.UserAddressMapper;
import com.ego.user.pojo.UserAddress;
import com.ego.user.service.UserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class UserAddressServiceImpl implements UserAddressService {
    @Resource
    private UserAddressMapper userAddressMapper;


    @Override
    @Transactional(readOnly = true)
    public List<UserAddress> getUserAddressList() {
        UserInfo userInfo = LoginInterceptor.getCurrUser();
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userInfo.getId());
        List<UserAddress> userAddressList = userAddressMapper.selectList(queryWrapper);

        return userAddressList;
    }

    @Override
    @Transactional
    public int add(UserAddress userAddress) {
        UserInfo userInfo = LoginInterceptor.getCurrUser();
        userAddress.setUserId(userInfo.getId());
        return userAddressMapper.insert(userAddress);
    }

    @Override
    @Transactional
    public int delete(Long id) {
        return userAddressMapper.deleteById(id);
    }

}
